package controllers;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;


import models.Notification;
import models.User;
import models.UserLocation;
import play.libs.WS;
import play.mvc.Controller;
import siena.Model;

import com.google.gson.JsonElement;

public class GPS extends Controller {

	/**
	 * Updates (or create, if this is the first time) the location of the user
	 * identified by <code>phone</code> using the passed coordinates
	 * 
	 * @author loai
	 * 
	 * @param phone
	 *            the phone of the user
	 * @param longitude
	 *            the longitude of the location of the user
	 * @param latitude
	 *            the latitude of the location of the user
	 * @throws ParseException 
	 * @throws UnsupportedEncodingException 
	 */
	public static void updateUserLocation(String phone, double longitude,
			double latitude,String cuTime) throws ParseException, UnsupportedEncodingException {
		
		DateFormat formater=DateFormat.getDateTimeInstance();
		Date now=formater.parse(URLDecoder.decode(cuTime,"UTF-8"));
		System.out.println("in update user location"+ cuTime +" "+now);
		System.out.println(phone + " " + longitude + " " + latitude);
		UserLocation.updateUserLocation(phone, longitude, latitude);
		Notification.markFulfilledRequests(phone, longitude, latitude,now);
	}

	public static void viewUsersLocations() {
		List<UserLocation> list = (List<UserLocation>) Model.all(
				UserLocation.class).fetch();
		ListIterator<UserLocation> iterator = list.listIterator();
		while (iterator.hasNext())
			iterator.next().user.get();
		int size = list.size();
		render(list, size);
	}



}
